package com.dbq.fast.business.product;

import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.dbq.fast.App;
import com.dbq.fast.R;
import com.dbq.fast.analysis.EventConstant;
import com.dbq.fast.analysis.YibaAnalysis;
import com.dbq.fast.business.product.api.CommonTransformer;
import com.dbq.fast.common.base.BaseActivity;
import com.dbq.fast.common.net.HttpManager;
import com.dbq.fast.util.SystemUtil;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * 使用wfee支付购买vpn服务
 */

public class WFEEActivity extends BaseActivity {

    @BindView(R.id.title)
    TextView title;

    @BindView(R.id.back)
    ImageView back;

    @BindView(R.id.unicron_wallet_addr)
    TextView walletAddr;

    @BindView(R.id.copy_wallet_addr)
    TextView copyWalletAddr;


    @BindView(R.id.ll_bottom_pay_button)
    LinearLayout bottomPayButton;

    private String walletAddrStr = "0xfd490c00217FBB06B076AeBa37486CFEC1DE0B43";

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_wfee);
        ButterKnife.bind(this);

        initUiData();
        initListener();
        loadData();


    }

    private void loadData() {

        HttpManager.getInstance().getHttpService()//
                .getWalletAddr()//
                .compose(new CommonTransformer<>())//
                .subscribe(walletAddressHttpResult -> {
                    if (walletAddressHttpResult.isSuccess()) {
                        walletAddrStr = walletAddressHttpResult.getData().wallet;
                        walletAddr.setText(walletAddrStr);
                    }
                }, throwable -> {
                });
    }

    private void initListener() {
        back.setOnClickListener(v -> onBackPressed());

        copyWalletAddr.setOnClickListener(v -> {
            Toast.makeText(WFEEActivity.this, R.string.copy, Toast.LENGTH_SHORT).show();
            SystemUtil.copyToBoad(walletAddrStr);

            YibaAnalysis.getInstance().event(App.getContext(), EventConstant.CLICK_WFEE_TOKEN_COPY);
        });

        bottomPayButton.setOnClickListener(v -> {
            if (!isQQClientAvailable(this)) {
                Toast.makeText(WFEEActivity.this, R.string.install_qq, Toast.LENGTH_SHORT).show();//使用wfee支付，是针对中国用户，所以提示是否安装qq
            } else {
                String url = "mqqwpa://im/chat?chat_type=wpa&uin=1490169409";//"http://wpa.qq.com/msgrd?v=3&uin=1490169409&site=qq&menu=yes";
                startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse(url)));
            }
        });

    }

    private void initUiData() {
        title.setText(R.string.wfee_pay);
        walletAddr.setText(walletAddrStr);
    }

    /**
     * 判断qq是否可用
     *
     * @param context
     * @return
     */
    public static boolean isQQClientAvailable(Context context) {
        final PackageManager packageManager = context.getPackageManager();
        List<PackageInfo> pinfo = packageManager.getInstalledPackages(0);
        if (pinfo != null) {
            for (int i = 0; i < pinfo.size(); i++) {
                String pn = pinfo.get(i).packageName;
                if (pn.equals("com.tencent.mobileqq")) {
                    return true;
                }
            }
        }
        return false;
    }


}
